#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <time.h>
#include <stdlib.h>
#include <syslog.h>
#include "SDL.h"
#include "PDL.h"

#include <pthread.h>

#include <SDL_events.h>

void *start_vibratortesting(void *ptr);

unsigned int vibra_duration = 1;
unsigned int vibra_suspension = 1;
unsigned int vibra_executions = 10;

char CBParam[128] = {0};

PDL_bool Quit(PDL_JSParameters *params)
{
	SDL_QuitEvent quitEvent;
	quitEvent.type = SDL_QUIT;
	SDL_PushEvent((SDL_Event*)&quitEvent);
	
	return PDL_TRUE;
}

PDL_bool RunVibratorTest(PDL_JSParameters *params)
{
	char reply[64];
    pthread_t 	thread_vibrator;
	int 		thread_vibrator_id;
  
   syslog(LOG_INFO, "SamDebug: %s\n", __func__);
  
	if (PDL_GetNumJSParams(params) < 3)
	{
		snprintf(reply, 64, "Vibrator Test: Invalid Params");
		PDL_JSReply(params, reply);
		return PDL_TRUE;
	}
	
	vibra_duration = (unsigned int)atoi(PDL_GetJSParamString(params, 0));
	vibra_suspension = (unsigned int)atoi(PDL_GetJSParamString(params, 1));
	vibra_executions = (unsigned int)atoi(PDL_GetJSParamString(params, 2));

	thread_vibrator_id = pthread_create(&thread_vibrator, NULL, start_vibratortesting, NULL);	
	if (thread_vibrator_id != 0){
		snprintf(reply, 64, "Vibrator Test Start Fail");
	} else {
		snprintf(reply, 64, "Vibrator Test Start OK");
	}	

	PDL_JSReply(params, reply);
	return PDL_TRUE;
}

int main(int argc, char** argv)
{
  openlog("com.hp.testvibrator", 0, LOG_USER);
  
  syslog(LOG_INFO, "SamDebug: main\n");
  
  system("rootfs_open");
  system("echo pardon > /etc/nojail");
    
  if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
  {
    printf("Could not init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  
  PDL_Init(0);
  
  syslog(LOG_INFO, "SamDebug: before registering js call back\n");
  
  // register the js callback
  PDL_RegisterJSHandler("RunVibratorTest", RunVibratorTest);
  PDL_RegisterJSHandler("Quit", Quit);
  PDL_JSRegistrationComplete();
  
  syslog(LOG_INFO, "SamDebug: completing registering js call back\n");
  
  // Event descriptor
  SDL_Event event;
  
  //Loop till SDL_QUIT comes
  do
  {
    SDL_PollEvent(&event);    
  } while (event.type != SDL_QUIT);
  
 
  // Cleanup
  PDL_Quit();
  SDL_Quit();
  closelog();
}



void *start_vibratortesting(void *ptr){
	const char *params[1];
	int i;
	
	system("echo 1 > /sys/class/vibrator/vib0/vib_enable");
	for (i = 0; i < vibra_executions; i++)
	{
		system("echo 1 > /sys/class/vibrator/vib0/vib_start");
		sleep(vibra_duration);
		system("echo 0 > /sys/class/vibrator/vib0/vib_start");
		sleep(vibra_suspension);
	}
	system("echo 0 > /sys/class/vibrator/vib0/vib_enable");
	
	sprintf(CBParam, "Vibrator Test Pass");
	params[0] = CBParam;
	PDL_Err mjErr = PDL_CallJS("VibratorTestCompleted", params, 1);
}
